package com.android.happymall.fragment.detail;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.happymall.R;
import com.android.happymall.activity.mine.TransparentActivity;
import com.android.happymall.fragment.BaseLazyFragment;
import com.android.happymall.model.member.AddressListData;
import com.android.happymall.model.member.AddressListDataCallback;
import com.android.happymall.utils.ConstantsUrl;
import com.android.happymall.utils.HttpUtils;
import com.android.happymall.utils.LoginState;
import com.android.happymall.utils.MyLog;
import com.android.happymall.utils.ObjectUtils;
import com.mcxtzhang.nestlistview.NestFullListView;
import com.mcxtzhang.nestlistview.NestFullListViewAdapter;
import com.mcxtzhang.nestlistview.NestFullViewHolder;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static android.app.Activity.RESULT_OK;


/**
 * 订单收货地址选择
 */
public class Fragment_Order_Address extends BaseLazyFragment {


    @BindView(R.id.listView)
    NestFullListView listView;

    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_address)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;

    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;

    private boolean networkConnected;

    private String getid;
    private String token;


    public Fragment_Order_Address() {
    }


    public static Fragment_Order_Address newInstance() {
        Fragment_Order_Address fragment = new Fragment_Order_Address();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {
    }


    @Override
    protected void initData() {
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_mine_address, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }


    private void initUI() {

        token = LoginState.getInstance().getUserInfo(mcontext,2);
        listView.setAdapter(new NestFullListViewAdapter<AddressListData.AddressData>(R.layout.item_list_mine_address, addressdata) {
            @Override
            public void onBind(int position, AddressListData.AddressData addressData, NestFullViewHolder holder) {

               // holder.setOnClickListener(R.id.item_list_mine_address_delete, new ClickUpdateAddress(0, position));
              //  holder.setOnClickListener(R.id.item_list_mine_address_xiugai, new ClickUpdateAddress(1, position));
                holder.setText(R.id.item_list_mine_address_name, addressData.getName() + "(" + addressData.getMobile() + ")");
                holder.setText(R.id.item_list_mine_address_address, addressData.getArea() + addressData.getAddress());
                if (addressData.getIs_default().equals("1")) {
                    holder.setChecked(R.id.item_list_mine_address_check, true);
                } else {
                    holder.setChecked(R.id.item_list_mine_address_check, false);
                }
            }
        });

        listView.setOnItemClickListener(new NestFullListView.OnItemClickListener() {
            @Override
            public void onItemClick(NestFullListView nestFullListView, View view, int position) {

                if (addressdata.get(position) != null) {
                    Intent data = new Intent();
                    data.putExtra("name",addressdata.get(position).getName());
                    data.putExtra("area",addressdata.get(position).getArea());
                    data.putExtra("mobile",addressdata.get(position).getMobile());
                    data.putExtra("address",addressdata.get(position).getAddress());
                    data.putExtra("id",addressdata.get(position).getId());
                    getActivity().setResult(RESULT_OK, data);
                    getActivity().finish();
                }
            }
        });


    }

    @OnClick({R.id.click_resetnetwork_refresh, R.id.address_add})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.click_resetnetwork_refresh:
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
                GetDataListData();
                break;
            case R.id.address_add:
                Intent intent = new Intent(mcontext, TransparentActivity.class);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_TRANS_SHOW_TYPE, 0);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_TRANS_NUMBER, 2);
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onResume() {
        super.onResume();

        GetDataListData();


    }


    ArrayList<AddressListData.AddressData> addressdata = new ArrayList<>();

    //获取列表数据
    private void GetDataListData() {

        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERADDRESSLIST;
        MyLog.e("地址", address);
        if (networkConnected) {
            HashMap<String, String> header = new HashMap<>();
            getid = LoginState.getInstance().getid(mcontext);
            header.put("UID", getid);
            header.put("TOKEN", token);
            HttpUtils.get(address, header, null, new AddressListDataCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                        }

                        @Override
                        public void onResponse(AddressListData response, int id) {
                            addressdata.clear();
                            ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                            if (response.getCode() == 0) {
                                if (response.getData() != null) {
                                    noData.setVisibility(View.GONE);
                                    addressdata.addAll(response.getData());
                                    listView.updateUI();
                                } else {
                                    noData.setVisibility(View.VISIBLE);
                                }
                            } else {
                                noData.setVisibility(View.VISIBLE);
                                noDataTv.setText(response.getMsg());
                                noDataTv.setVisibility(View.VISIBLE);
                            }

                        }
                    }

            );
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }
}
